package com.example.ch04.menu;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ch04.R;

public class OptionMenusActivity extends AppCompatActivity {

    //Initialize this in onCreateOptions
    Menu myMenu = null;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_options);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //call the parent to attach any system level menus
        super.onCreateOptionsMenu(menu);
        this.myMenu = menu;
        //add a few normal menus
        addRegularMenuItems(menu);
        //add a few secondary menus
        addSecondaryMenuItems(menu);
        addSubMenu(menu);
        //it must return true to show the menu
        //if it is false menu won't show
        return true;
    }

    private void addRegularMenuItems(Menu menu) {
        //Secondary items are shown just like everything else
        int base = Menu.FIRST; // value is 1

        MenuItem item1 = menu.add(base, base, base, "Simple Menu");
        menu.add(base, base + 1, base + 1, "XML Menu");
        menu.add(base, base + 2, base + 2, "Clear");

        menu.add(base, base + 3, base + 3, "Hide secondary");
        menu.add(base, base + 4, base + 4, "Show secondary");

        menu.add(base, base + 5, base + 5, "Enable secondary");
        menu.add(base, base + 6, base + 6, "Disable secondary");

        menu.add(base, base + 7, base + 7, "Check secondary");
        MenuItem item8 = menu.add(base, base + 8, base + 8, "Uncheck secondary");
        //This will show the icon
        //It might obscure the text
        item1.setIcon(R.drawable.balloons);
        //But this does not
        item8.setIcon(R.drawable.balloons);
    }

    private void addSecondaryMenuItems(Menu menu) {
        //Secondary items are shown just like everything else
        int base = Menu.CATEGORY_SECONDARY;

        menu.add(base, base + 1, base + 1, "Sec. Item 1");
        menu.add(base, base + 2, base + 2, "Sec. Item 2");
        menu.add(base, base + 3, base + 3, "Sec. Item 3");
        menu.add(base, base + 3, base + 3, "Sec. Item 4");
        menu.add(base, base + 4, base + 4, "Sec. Item 5");
    }

    private void addSubMenu(Menu menu) {
        //Secondary items are shown just like everything else
        int base = Menu.FIRST + 100;
        SubMenu sm = menu.addSubMenu(base, base + 1, Menu.NONE, "Submenu");
        MenuItem item1 = sm.add(base, base + 2, base + 2, "Sub Item1");
        sm.add(base, base + 3, base + 3, "Sub Item2");
        sm.add(base, base + 4, base + 4, "Sub Item3");
        //work the icons
        //submenu item icons are not supported
        item1.setIcon(R.drawable.icon48x48_2);
        sm.setIcon(R.drawable.icon48x48_1);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == 1) {
            appendText("\nSimple Menu");
        } else if (item.getItemId() == 2) {
            this.appendMenuItemText(item);
            Intent intent = new Intent(this, XMLMenusActivity.class);
            this.startActivity(intent);
        } else if (item.getItemId() == 3) {
            emptyText();
        } else if (item.getItemId() == 4) {
            //hide secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
        } else if (item.getItemId() == 5) {
            //show secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
        } else if (item.getItemId() == 6) {
            //enable secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
        } else if (item.getItemId() == 7) {
            //disable secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
        } else if (item.getItemId() == 8) {
            //check secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
        } else if (item.getItemId() == 9) {
            //uncheck secondary
            this.appendMenuItemText(item);
            this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
        } else {
            this.appendMenuItemText(item);
        }
        //should return true if the menu item
        //is handled
        return true;
    }

    private TextView getTextView() {
        TextView tv =
                (TextView) this.findViewById(R.id.textViewId);
        return tv;
    }

    public void appendText(String text) {
        TextView tv =
                (TextView) this.findViewById(R.id.textViewId);
        tv.setText(tv.getText() + text);
    }

    private void appendMenuItemText(MenuItem menuItem) {
        String title = menuItem.getTitle().toString();
        TextView tv =
                (TextView) this.findViewById(R.id.textViewId);
        tv.setText(tv.getText() + "\n" + title + ":" + menuItem.getItemId());
    }

    private void emptyText() {
        TextView tv =
                (TextView) this.findViewById(R.id.textViewId);
        tv.setText("");
    }
}